package com.example.baidu;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    private static final String API_KEY = "U3zD1H6KeDxbIKMz2HCRuZHX";
    private static final String APP_ID = "35471330";
    private static final String SECRET_KEY = "rvgtOU0FGvqGmHOZfPhkKby31qmGwaUi";

    public static Map businessLicense(String imageBase64) {
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        HashMap<String, String> options = new HashMap<String, String>();
        JSONObject res = client.businessLicense(imageBase64, options);
        String str = res.get("words_result").toString();
        Map map = JSONUtil.parseObj(str);
        String s1 = toValue(map.get("法人").toString());
        map.put("法人", s1);
        String s2 = toValue(map.get("社会信用代码").toString());
        map.put("社会信用代码", s2);
        String s3 = toValue(map.get("证件编号").toString());
        map.put("证件编号", s3);
        String s4 = toValue(map.get("单位名称").toString());
        map.put("单位名称", s4);
        return map;
    }

    public static String threeFactorsVerification(Map mapParam) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/three_factors_verification";
        try {
            String param = "name=" + mapParam.get("法人") + "&company=" + mapParam.get("单位名称") + "&regnum=" + mapParam.get("社会信用代码");

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken =AuthService.getAuth(API_KEY, SECRET_KEY);

            String result = HttpUtil.post(url, accessToken, param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toValue(String mes) {
        Map map = JSONUtil.parseObj(mes);
        return map.get("words").toString();
    }

    public static void main(String[] args) {
        Map map = Test.businessLicense("D:\\1\\123.jpg");
        System.out.println(Test.threeFactorsVerification(map));
    }

}
